package com.occludens.admin.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Load;
import com.googlecode.objectify.annotation.Subclass;

@Subclass(index=true)
@Cache
public class Table extends AdminObjectUI {
	@Load private List<Ref<Column>> columns = new ArrayList<Ref<Column>>();
	
	protected Table() {} // Mandatory for Objectify
	
	public Table(String name) {
		super(name);
	}
	
	public List<Column> getColumns() {
		List<Column> returnList = new ArrayList<Column>();
		Iterator<Ref<Column>> columnsItr = this.columns.iterator();
		while (columnsItr.hasNext()) {
			returnList.add(columnsItr.next().get());
		}//End of while columns
		return returnList;
	}

	public void setColumns(List<Column> columns) {
		Iterator<Column> columnsItr = columns.iterator();
		while (columnsItr.hasNext()) {
			Ref<Column> columnRef = Ref.create(columnsItr.next());
			if (!this.columns.contains(columnRef)) {
				this.columns.add(columnRef);
			}
		}//End of while columnsItr
	}

	public void addColumns(Column... columns) {
		for (int i=0; i<columns.length; i++) {
			Ref<Column> columnRef = Ref.create(columns[i]);
			if (!this.columns.contains(columnRef)) {
				this.columns.add(columnRef);
			}
		}
	}

	public void removeColumns(Column... columns) {
		for (int i=0; i<columns.length; i++) {
			Ref<Column> columnRef = Ref.create(columns[i]);
			if (this.columns.contains(columnRef)) {
				this.columns.remove(columnRef);
			}
		}
	}
}
